package com.kingyea.mobilepolice.apimanage.responseinfo.service.impl;

import com.kingyea.mobilepolice.apimanage.responseinfo.bean.ResponseInfo;
import com.kingyea.mobilepolice.apimanage.responseinfo.dao.ResponseInfoMapper;
import com.kingyea.mobilepolice.apimanage.responseinfo.service.ResponseInfoService;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.NumberUtils;
import com.kingyea.mobilepolice.apimanage.utils.StateCode;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description ResponseInfo 实现类
 * @date Created in 2018-03-11 11:19:17
 */
@Service
@Transactional
public class ResponseInfoServiceImpl implements ResponseInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseInfoServiceImpl.class);

    @Resource
    private ResponseInfoMapper responseInfoMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public ResponseInfo getById(Integer id) throws Exception {
        return this.responseInfoMapper.getById(id);
    }

    @Override
    public List<ResponseInfo> getByServiceId(Integer serviceId) throws Exception {
        return responseInfoMapper.getByServiceId(serviceId);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<ResponseInfo> list(ResponseInfo responseInfo) throws Exception {
        List<ResponseInfo> list = responseInfoMapper.list(responseInfo);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Integer id) throws Exception {
        responseInfoMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param responseInfo
     * @return
     */
    @Send2Queue
    @Override
    public ResponseInfo save(ResponseInfo responseInfo) throws Exception {
        if (StringUtils.isEmpty(responseInfo.getName()) || StringUtils.isEmpty(responseInfo.getServiceId()) || NumberUtils.isNullOrLEZero(responseInfo.getServiceId())) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        // 检查数据重复
        ResponseInfo isExistResp = responseInfoMapper.getByNameAndServiceId(responseInfo.getName(), responseInfo.getServiceId());
        if (isExistResp != null) {
            throw new BusinessException(StateCode.SERVER_DATA_DUPLICATE_ERROR);
        }
        responseInfoMapper.save(responseInfo);
        return responseInfo;
    }

    /**
     * 更新
     *
     * @param responseInfo
     * @return
     */
    @Send2Queue
    @Override
    public ResponseInfo update(ResponseInfo responseInfo) throws Exception {
        if (StringUtils.isEmpty(responseInfo.getName()) || StringUtils.isEmpty(responseInfo.getServiceId()) || NumberUtils.isNullOrLEZero(responseInfo.getServiceId())) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        // 检查除了自己外有没有其它记录用了 name 和 serviceId 组合主键
        ResponseInfo isExistObj = responseInfoMapper.getByNameAndServiceIdAndExcludeID(responseInfo.getName(), responseInfo.getServiceId(), responseInfo.getId());
        if (isExistObj != null) {
            throw new BusinessException(StateCode.SERVER_DATA_DUPLICATE_ERROR);
        }
        responseInfoMapper.update(responseInfo);
        return responseInfo;
    }

    /**
     * 根据接口id删除相关响应参数信息
     */
    @Send2Queue
    @Override
    public void deletesByServiceId(Integer serviceId) {
        responseInfoMapper.deletesByServiceId(serviceId);
    }

    @Override
    public ResponseInfo getByNameAndServiceId(String name, Integer serviceId) {
        return responseInfoMapper.getByNameAndServiceId(name, serviceId);
    }


}
